
***********************************************
* Authors: Henrik Andersson & Sirus Dehdari
* This do-file generates all regressions, presented in the main paper    *
* as well as the online appendix, section C
*************************

clear all
set more off

set maxvar 10000
set matsize 11000

use "E:\ProjData\IntegrationSD\use\main.dta"

sort precinct2014 t
///GENERATE SOME MORE VARIABLES

///THE POLITICAL MEASURE (PERCENT) ARE DIFFERENT IN 10 AND 14 - CHANGE
foreach y in Mproc Cproc FPproc KDproc Sproc Vproc MPproc SDproc FIproc PPproc {
replace `y' = `y'*100 if year==2014
}
*Dependent
gen dSD = d.SDproc

*Main independent
gen employment_share = employed_other/other_im
gen emp_proc_other = employment_share*100
gen unempl_other_share = unemployed_other/other_im
gen unem_proc_other = unempl_other_share*100
gen unem_proc_swed = (unemployed_swedish/swedish)*100

*Other variables
gen nordic_share = (nordic/pop)*100
gen swedish_share = (swedish/pop)*100
gen western_share = (western/pop)*100
gen europe_other_share = (europe_other/pop)*100
gen other_share = (other_im/pop)*100

gen low_educ_share = (low_educ/pop)*100
gen high_educ_share = (high_educ/pop)*100

gen pop2 = pop^2

gen ctz_other_share = (ctz_other/other_im)*100




gen logwage = ln(LoneInk)
*share young
gen young_share = (young/pop)*100

*Workerdata
gen dother_high_educ = d.other_high_educ
gen other_high_educ_share = (other_high_educ/other_im)*100
gen other_low_educ_share = (other_low_educ/other_im)*100
gen dother_high_educ_share = d.other_high_educ_share

replace share_im_cworker_swe = share_im_cworker_swe*100
replace sh_im_cw_noi_swe = sh_im_cw_noi_swe*100
replace matchedcoworkers = matchedcoworkers*100
replace share_matchedhigh_cworker = share_matchedhigh_cworker*100
replace share_matchedlow_cworker = share_matchedlow_cworker*100

replace crossskill = crossskill*100
replace sharecrossskill_lownative = sharecrossskill_lownative*100
replace sharecrossskill_highnative = sharecrossskill_highnative*100


replace share_im_cworker_swe_stayed = share_im_cworker_swe_stayed*100

///regions
foreach x in latin mena asia africa rest nordic western {
replace share_`x'_cworker_swe = share_`x'_cworker_swe*100
}

///TYPE YEAR
foreach x in newim mediumnewim notnewim oldim highskother lowskother {
replace share_`x'_cworker_swe = share_`x'_cworker_swe*100
}

///sector:
replace sector_share_im = sector_share_im*100


*Municipality
gen str8 str_dstr = string(precinct2014,"%08.0f")
gen kom_str = substr(str_dstr, 1,4)
destring kom_str, replace

*Region 
gen lan_str = substr(str_dstr, 1,2)
destring lan_str, replace

*Add on labor market regions
merge m:1 kom_str using "E:\ProjData\IntegrationSD\temp\lmr2014"
drop _merge 
encode Kod, gen(lmr14)
drop Kod


*Placebo measure - blank votes
replace BLproc = BLANKproc if year==2006
drop BLANKproc

gen marr_proc = (new_married/pop)*100
gen divorce_proc = (new_divorce/pop)*100

gen men_proc = (men/pop)*100




*LABELS

label variable SDproc  "$\%$ votes for the SD"
label variable dSD  "Votes for the SD, $\Delta$ $\%$-units"
label variable share_im_cworker_swe  "$\%$ non-European coworkers among Swedes"
label variable im_coworker_swe  "$\#$ imm-coworkers among Swedes"
label variable share_im_cworker_swe_stayed  "percent imm-coworkers among non-moving Swedes"
label variable im_coworker_swe_stayed  "# imm-coworkers among non-moving Swedes"
label variable pop "Population"
label variable low_educ_share "$\%$ low educated"
label variable ALosDag  "$\#$ of unemployment benefit days"
label variable ctz_other_share  "$\%$ citizens of non-Europeans"
label variable unem_proc_swed  "$\%$ non-working swedes"
label variable other_share  "$\%$ non-Europeans in precinct"
label variable other_low_educ_share  "$\%$ low educ. non-Europeans in precinct"
label variable deviation_sector  "Deviation from national level"
label variable young_share  "$\%$ young"
*regions
label variable share_latin_cworker_swe  "$\%$ Latin American"
label variable latin_coworker_swe  "# Latin American"
label variable share_mena_cworker_swe  "$\%$ MENA"
label variable mena_coworker_swe  "# MENA"
label variable share_asia_cworker_swe  "$\%$ Other Asia"
label variable asia_coworker_swe  "# Other Asia"
label variable share_africa_cworker_swe  "$\%$ Other Africa"
label variable africa_coworker_swe  "# Other Africa"
label variable share_rest_cworker_swe  "$\%$ Oceania and Stateless"
label variable rest_coworker_swe  "# Oceania and Stateless"
  
label variable share_nordic_cworker_swe  "$\%$ Nordic"
label variable nordic_coworker_swe  "# Nordic"
label variable share_western_cworker_swe  "$\%$ Western"
label variable western_coworker_swe  "# Western"

*other worker characteristics
label variable share_newim_cworker_swe  "$\leq$5 years in country"
label variable share_mediumnewim_cworker_swe "$\leq$10 years, $\geq$5 years in country"
label variable share_notnewim_cworker_swe "$\leq$15 years, $\geq$10 years in country"
label variable share_oldim_cworker_swe  "$\geq$15 years in country"                

label variable share_highskother_cworker "$\%$ high skilled"
label variable share_lowskother_cworker "$\%$ low skilled"

*other worker characteristics
label variable share_newim_cworker_swe  "$\leq$5 years in country"
label variable share_mediumnewim_cworker_swe "$\leq$10 years, $\geq$5 years in country"
label variable share_notnewim_cworker_swe "$\leq$15 years, $\geq$10 years in country"
label variable share_oldim_cworker_swe  "$\geq$15 years in country"                

label variable share_highskother_cworker_swe "$\%$ high skilled"
label variable share_lowskother_cworker_swe "$\%$ low skilled"



***************
* ANALYSIS *
*************

*************************
* baseline table  Table 1*
*************************

*standard explanatory variable
egen std_treat = std(sh_im_cw_noi_swe)
*gen std_treat = sh_im_cw_noi_swe
label variable std_treat   "WP contact with \\non-Europeans" 

*matched expl.
egen std_treatmatched = std(matchedcw)
*gen std_treatmatched = matchedcw*100
label variable std_treatmatched   "WP contact with same \\skill non-Europeans" 

*non-matched expl.
egen std_treatcrossskill = std(crossskill)
*gen std_treatcrossskill = crossskill*100
label variable std_treatcrossskill   "WP contact with opposite \\skill non-Europeans" 



eststo clear

eststo: xtreg SDproc std_treat pop pop2 low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share i.year, fe vce(cluster precinct2014)

eststo: xtreg SDproc std_treat pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share i.year##i.lmr14, fe vce(cluster precinct2014)

eststo: xtreg SDproc std_treat pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp i.year##i.lmr14, fe vce(cluster precinct2014)

eststo: xtreg SDproc std_treatmatched std_treatcrossskill pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp i.year##i.lmr14, fe vce(cluster precinct2014)

esttab using "C:\Userdata\Shared\Output\IntegrationSD\regbasicnofloat.tex", ///
se noconstant compress label scalars(N) nonumbers /// 
keep(std_treat std_treatmatched std_treatcrossskill) fragment /// 
nonotes mlabels(none) ///
replace 

esttab using "C:\Users\henrik\Dropbox\IntregrationSD\Output\Tables\regbasicnofloat.tex", ///
se noconstant compress label scalars(N) nonumbers /// 
keep(std_treat std_treatmatched std_treatcrossskill) fragment /// 
nonotes mlabels(none) ///
replace  




***********************************************
*look at above and below median size of workplace - Table 2 *
**********************************************
///regular
egen std_treat_smwp = std(share_im_cworker_swe_smwp)
label variable std_treat_smwp   "WP contact with non-Europeans, \\small WP"

egen std_treat_bigwp = std(share_im_cworker_swe_bigwp)
label variable std_treat_bigwp   "WP contact with non-Europeans,\\large WP"

///more intense contact

egen std_treat_bigwp_match = std(share_matched_big)
label variable std_treat_bigwp_match   "WP contact with same-skill \\non-Europeans, large WP" 

egen std_treat_bigwp_nonm = std(share_cross_big)
label variable std_treat_bigwp_nonm     "WP contact with opposite-skill \\non-Europeans, large WP"  

egen std_treat_smwp_match = std(share_matched_small)
label variable std_treat_smwp_match   "WP contact with same-skill \\non-Europeans, small WP" 

egen std_treat_smwp_nonm = std(share_cross_small)
label variable std_treat_smwp_nonm     "WP contact with opposite-skill \\non-Europeans, small WP"  



///i samma regression
eststo clear
eststo: xtreg SDproc std_treat_smwp std_treat_bigwp pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share workers_swe wage_cw_wp share_male_cw_wp share_young_cw_wp  i.year##i.lmr14, fe vce(cluster precinct2014)

eststo: xtreg SDproc std_treat_bigwp_match std_treat_bigwp_nonm std_treat_smwp_match std_treat_smwp_nonm pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share workers_swe wage_cw_wp share_male_cw_wp share_young_cw_wp  i.year##i.lmr14, fe vce(cluster precinct2014)

esttab using "C:\Userdata\Shared\Output\IntegrationSD\regsmallbigwpnofloat.tex", ///
se noconstant compress label scalars(N) nonumbers /// 
keep(std_treat_smwp std_treat_bigwp std_treat_bigwp_match std_treat_bigwp_nonm std_treat_smwp_match std_treat_smwp_nonm) fragment /// 
nonotes mlabels(none) ///
replace 

esttab using "C:\Users\henrik\Dropbox\IntregrationSD\Output\Tables\regsmallbigwpnofloat.tex", ///
se noconstant compress label scalars(N) nonumbers /// 
keep(std_treat_smwp std_treat_bigwp std_treat_bigwp_match std_treat_bigwp_nonm std_treat_smwp_match std_treat_smwp_nonm) fragment /// 
nonotes mlabels(none) ///
replace 



////////////////
//Type of occupation
////////////////

///separated by RTI - all - Table 4

egen std_topRTI = std(share_topRTI)
label variable std_topRTI   "$\%$ im. co-workers \\vulnerable occupations" 
egen std_bottomRTI = std(share_bottomRTI)
label variable std_bottomRTI   "$\%$ im. co-workers \\non-vulnerable occupations" 


eststo clear

eststo: xtreg SDproc std_topRTI pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp i.year##i.lmr14, fe vce(cluster precinct2014)

eststo: xtreg SDproc std_bottomRTI pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp i.year##i.lmr14, fe vce(cluster precinct2014)

esttab using "C:\Userdata\Shared\Output\IntegrationSD\RTI.tex", ///
se noconstant compress label fragment scalars(N) nonumbers /// 
keep(std_topRTI std_bottomRTI) /// 
nonotes mlabels(none) ///
replace 

esttab using "C:\Users\henrik\Dropbox\IntregrationSD\Output\Tables\RTI.tex", ///
se noconstant compress label fragment scalars(N) nonumbers /// 
keep(std_topRTI std_bottomRTI) /// 
nonotes mlabels(none) ///
replace 


///one table
eststo clear

eststo: xtreg SDproc std_topRTI std_bottomRTI pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp i.year##i.lmr14, fe vce(cluster precinct2014)

esttab using "C:\Userdata\Shared\Output\IntegrationSD\RTI_alt.tex", ///
se noconstant compress label fragment scalars(N) /// 
keep(std_topRTI std_bottomRTI) /// 
nonotes mlabels(none) ///
replace 





/// ONLINE APPENDIX!


/////Comment reviewer 2 APSR: try it without the precinct fixed effects - RE

*(Table C8)

eststo clear

eststo: xtreg SDproc std_treat i.year##i.lmr14, fe vce(cluster precinct2014)

eststo: xtreg SDproc std_treatmatched std_treatcrossskill i.year##i.lmr14, fe vce(cluster precinct2014)

eststo: xtreg SDproc std_treat pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp i.year, vce(cluster precinct2014)

eststo: xtreg SDproc std_treatmatched std_treatcrossskill pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp i.year, vce(cluster precinct2014)


esttab using "C:\Userdata\Shared\Output\IntegrationSD\regbasicnofloat_nofe_fe.tex", ///
se noconstant compress label scalars(N) nonumbers /// 
keep(std_treat std_treatmatched std_treatcrossskill) fragment /// 
nonotes mlabels(none) ///
replace 

esttab using "C:\Users\henrik\Dropbox\IntregrationSD\Output\Tables\regbasicnofloat_nofe_fe.tex", ///
se noconstant compress label scalars(N) nonumbers /// 
keep(std_treat std_treatmatched std_treatcrossskill) fragment /// 
nonotes mlabels(none) ///
replace  




///////////////
// OUTLIERS //
///////////////

///dropping the top ten most populated distrcit and top ten lowest populated
///Table C1 and C2
gen pop2014 = pop if year==2014
cumul pop2014, gen(cumpop)
bysort precinct2014: egen cumpop2014 = max(cumpop)

*as well as the top ten who increased the most and th top then who dropped the most
sort precinct2014 t
bysort precinct2014: gen popdiff = pop-l2.pop
cumul popdiff, gen(cumpopdiff)
bysort precinct2014: egen cumpopdiff2014 = max(cumpopdiff)



///NO FLOAT

eststo clear

eststo: xtreg SDproc std_treat pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp  i.year##i.lmr14 if cumpop2014<0.90 ///
, fe vce(cluster precinct2014)

eststo: xtreg SDproc std_treat pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp  i.year##i.lmr14 if cumpop2014>0.10 ///
, fe vce(cluster precinct2014)

eststo: xtreg SDproc std_treat pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp  i.year##i.lmr14 if cumpopdiff2014<0.90 ///
, fe vce(cluster precinct2014)

eststo: xtreg SDproc std_treat pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp  i.year##i.lmr14 if cumpopdiff2014>0.10 ///
, fe vce(cluster precinct2014)

esttab using "C:\Userdata\Shared\Output\IntegrationSD\reg_popnofloat.tex", ///
se noconstant fragment compress label scalars(N) nonum nodep /// 
keep(std_treat) /// 
nonotes ///
replace 

esttab using "C:\Users\henrik\Dropbox\IntregrationSD\Output\Tables\reg_popnofloat.tex", ///
se noconstant fragment compress label scalars(N) nonum nodep /// 
keep(std_treat) /// 
nonotes ///
replace 


///Same for same skill 

///NO FLOAT
eststo clear

eststo: xtreg SDproc std_treatmatched std_treatcrossskill logwage pop pop2 low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp  i.year##i.lmr14 if cumpop2014<0.90 ///
, fe vce(cluster precinct2014)

eststo: xtreg SDproc std_treatmatched std_treatcrossskill logwage pop pop2 low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp  i.year##i.lmr14 if cumpop2014>0.10 ///
, fe vce(cluster precinct2014)

eststo: xtreg SDproc std_treatmatched std_treatcrossskill logwage pop pop2 low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp  i.year##i.lmr14 if cumpopdiff2014<0.90 ///
, fe vce(cluster precinct2014)

eststo: xtreg SDproc std_treatmatched std_treatcrossskill logwage pop pop2 low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp  i.year##i.lmr14 if cumpopdiff2014>0.10 ///
, fe vce(cluster precinct2014)

esttab using "C:\Userdata\Shared\Output\IntegrationSD\reg_popskillnofloat.tex", ///
se noconstant fragment compress label scalars(N) nonum nodep /// 
keep(std_treatmatched std_treatcrossskill) /// 
nonotes ///
replace 

esttab using "C:\Users\henrik\Dropbox\IntregrationSD\Output\Tables\reg_popskillnofloat.tex", ///
se noconstant fragment compress label scalars(N) nonum nodep /// 
keep(std_treatmatched std_treatcrossskill) /// 
nonotes ///
replace 




///DROP BASED ON OBSERVED OUTLIERS - take out based on residuals
* Table C3
*Take out outliers
eststo clear

eststo: xtreg SDproc std_treat pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp i.year##i.lmr14, fe vce(cluster precinct2014)

predict resid, resid

egen stdresid = std(resid) 

eststo clear


*Baseline estimate dropping -+2 std resid
eststo: xtreg SDproc std_treat pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp  i.year##i.lmr14 if stdresid<=2 & stdresid>=-2  ///
, fe vce(cluster precinct2014)
*same but considering +-1,5 as cut-off.
eststo: xtreg SDproc std_treat pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp  i.year##i.lmr14 if stdresid<=1.5 & stdresid<=1.5  ///
, fe vce(cluster precinct2014)


*same but considering within skill
eststo: xtreg SDproc std_treatmatched std_treatcrossskill pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp i.year##i.lmr14 if stdresid<=2 & stdresid>=-2, fe vce(cluster precinct2014)

eststo: xtreg SDproc std_treatmatched std_treatcrossskill pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp i.year##i.lmr14 if stdresid<=1.5 & stdresid<=1.5, fe vce(cluster precinct2014)




esttab using "C:\Userdata\Shared\Output\IntegrationSD\reg_outlier.tex", ///
se noconstant fragment compress label scalars(N) nonum nodep /// 
keep(std_treat std_treatmatched std_treatcrossskill) /// 
nonotes ///
replace 

esttab using "C:\Users\henrik\Dropbox\IntregrationSD\Output\Tables\reg_outlier.tex", ///
se noconstant fragment compress label scalars(N) nonum nodep /// 
keep(std_treat std_treatmatched std_treatcrossskill) /// 
nonotes ///
replace 



///hat_values:
*Table C4  

*First take out the values of dffits and hat values

reg SDproc std_treat pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp i.precinct2014 i.year##i.lmr14

predict dfits, dfits
*Simple cut off with those above 2 in value
gen cutoffdffits = 0
replace cutoffdffits = 1 if abs(dfits)>2 & dfits!=.

reg SDproc std_treat pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp i.precinct2014 i.year##i.lmr14

predict hat, hat
*Create cut-off, where we define all who have more than twice the mean of hat values as 1, the rest 0
egen m = mean(hat)
gen m2 = 2*m
gen cut_off_hat=0 
replace cut_off_hat = 1 if hat>m2
drop m m2

*Create a cut-off based on top 1 percent
cumul hat, gen(hatcum)
gen cut_off_hat2=0 
replace cut_off_hat2 = 1 if hatcum>=0.975


*What if we think of this in terms of panels? 
bysort precinct2014: egen newcutof_hat = max(cut_off_hat)
bysort precinct2014: egen newcutof_hat2 = max(cut_off_hat2)
bysort precinct2014: egen newcutof_df = max(cutoffdffits)



eststo clear


*baseline estimates without high leverage points
eststo: xtreg SDproc std_treat pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp  i.year##i.lmr14 if newcutof_hat==0  ///
, fe vce(cluster precinct2014)


*hatvalues - within skill
eststo: xtreg SDproc std_treatmatched std_treatcrossskill pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp  i.year##i.lmr14 if newcutof_hat==0  ///
, fe vce(cluster precinct2014)


*baseline estimates without high leverage points
eststo: xtreg SDproc std_treat pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp  i.year##i.lmr14 if newcutof_hat2==0  ///
, fe vce(cluster precinct2014)


*hatvalues - within skill
eststo: xtreg SDproc std_treatmatched std_treatcrossskill pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp  i.year##i.lmr14 if newcutof_hat2==0  ///
, fe vce(cluster precinct2014)



esttab using "C:\Users\henrik\Dropbox\IntregrationSD\Output\Tables\reg_leverage.tex", ///
se noconstant fragment compress label scalars(N) nonum nodep /// 
keep(std_treat std_treatmatched std_treatcrossskill) /// 
nonotes ///
replace 


////////////////////////
///////////////"PLACEBO"
/////////////////////


///lAGGED and DEVIATION

///deviation from national trend
egen deviation_std = std(deviation_sector)
label variable deviation_std   "Deviation from national sector" 


///Those who stayed

egen std_treatstay = std(sh_im_cw_noi_swe_stay)
label variable std_treatstay   "WP contact with \\non-Europeans, stayers"  

///coworkers in general
egen std_treat_cw = std(workers_swe)
label variable std_treat_cw   "WP contact with \\any coworkers"  


*Table C7
eststo clear


sort precinct2014 t
eststo: xtreg SDproc std_treat_cw pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp i.year##i.lmr14, fe vce(cluster precinct2014)
eststo: xtreg SDproc deviation_std pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp i.year##i.lmr14, fe vce(cluster precinct2014)
eststo: xtreg SDproc std_treatstay pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp  i.year##i.lmr14, fe vce(cluster precinct2014)

esttab using "C:\Userdata\Shared\Output\IntegrationSD\regplacebo.tex", ///
se noconstant compress label fragment scalars(N) nonum /// 
keep(std_treat_cw deviation_std std_treatstay)  /// 
nonotes  ///
replace 

esttab using "C:\Users\henrik\Dropbox\IntregrationSD\Output\Tables\regplacebo.tex", ///
se noconstant compress label fragment scalars(N) nonum /// 
keep(std_treat_cw deviation_std std_treatstay)  /// 
nonotes  ///
replace 


////REVIEWER COMMENTS: LAGGED DEPENDET VARIABLE
*Table C6
eststo clear

eststo: xtreg SDproc l.SDproc std_treat pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp i.year##i.lmr14, fe vce(cluster precinct2014)

eststo: xtreg SDproc std_treat pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp i.year##i.lmr14 if year==2010 | year==2014, fe vce(cluster precinct2014)


esttab using "C:\Userdata\Shared\Output\IntegrationSD\regdynamic.tex", ///
se noconstant compress label fragment scalars(N) nonum  /// 
keep(std_treat)  /// 
nonotes  ///
replace 

esttab using "C:\Users\henrik\Dropbox\IntregrationSD\Output\Tables\regdynamic.tex", ///
se noconstant compress label fragment scalars(N) nonum /// 
keep(std_treat)  /// 
nonotes ///
replace



/////REVIEWER COMMENTS: COMPARE LAGGED VALUES FOR SD not with zero but with baseline coefficient


sort precinct2014 t
xtreg l.SDproc std_treat pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp i.year##i.lmr14, fe vce(cluster precinct2014)
est store placebo

xtreg SDproc std_treat pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp i.year##i.lmr14, fe vce(cluster precinct2014)
est store baseline

///doesnt work with xtreg
*Just look at the confidence intervals?




///MORE PLACEBOS - OTHER PARTIES AND BLANK VOTES
*Table C5

eststo clear
sort precinct2014 t

eststo: xtreg l.SDproc std_treat pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp i.year##i.lmr14, fe vce(cluster precinct2014)
eststo: xtreg PPproc std_treat pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp i.year##i.lmr14, fe vce(cluster precinct2014)
eststo: xtreg FIproc std_treat pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp i.year##i.lmr14, fe vce(cluster precinct2014)
eststo: xtreg men_proc std_treat pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp i.year##i.lmr14, fe vce(cluster precinct2014)
eststo: xtreg marr_proc std_treat pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp i.year##i.lmr14, fe vce(cluster precinct2014)
eststo: xtreg divorce_proc std_treat pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp i.year##i.lmr14, fe vce(cluster precinct2014)
eststo: xtreg BLproc std_treat pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp i.year##i.lmr14, fe vce(cluster precinct2014)
eststo: xtreg BLproc std_treat pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp i.year##i.lmr14 if BLproc<=3, fe vce(cluster precinct2014)

esttab using "C:\Userdata\Shared\Output\IntegrationSD\regplacebo_otherparties.tex", ///
se noconstant compress label fragment scalars(N) nonum  /// 
keep(std_treat) mlabels(none) /// 
nonotes  ///
replace 

esttab using "C:\Users\henrik\Dropbox\IntregrationSD\Output\Tables\regplacebo_otherparties.tex", ///
se noconstant compress label fragment scalars(N) nonum /// 
keep(std_treat)  /// 
nonotes mlabels(none) nonum nodep ///
replace 


////REVIEWER COMMENT, TRY ANALYSIS WITH NO RESTRICTIONS on sample

*Table C9
///regular - no float environment for paper

*standard explanatory variable
egen std_treat_rob = std(sh_im_cw_noi_swe_rob)
*gen std_treat = sh_im_cw_noi_swe
label variable std_treat_rob   "WP contact with \\non-Europeans" 


eststo clear

eststo: xtreg SDproc std_treat_rob pop pop2 low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share i.year, fe vce(cluster precinct2014)

eststo: xtreg SDproc std_treat_rob pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share i.year##i.lmr14, fe vce(cluster precinct2014)

eststo: xtreg SDproc std_treat_rob pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp i.year##i.lmr14, fe vce(cluster precinct2014)


esttab using "C:\Userdata\Shared\Output\IntegrationSD\regbasicnofloat_rob.tex", ///
se noconstant compress label scalars(N) nonum /// 
keep(std_treat_rob) fragment /// 
nonotes mlabels(none) ///
replace 

esttab using "C:\Users\henrik\Dropbox\IntregrationSD\Output\Tables\regbasicnofloat_rob.tex", ///
se noconstant compress label scalars(N) nonum /// 
keep(std_treat_rob) fragment /// 
nonotes mlabels(none) ///
replace  



///final re-shuffling test
*Figure C1

set seed 2803 
foreach y of numlist 1(1)1000 {
gen double shuffle = runiform() 
sort kom_str year shuffle
bysort kom_str year: gen which = _n
sort kom_str year SDproc
gen sd_new_`y' = SDproc[which] 
drop shuffle which
}

set matsize 11000

	matrix A = J(8000,4,.)
* Next, loop over numbers from 5-25 and run the regression for each bandwidth.


	forvalues y=1/1000{

	 *scalar foreignshare = `n'
xtreg sd_new_`y' std_treat pop pop2 logwage low_educ_share ALosDag ctz_other_share ///
unem_proc_swed other_share other_low_educ_share wage_cw_wp share_male_cw_wp share_young_cw_wp i.year##i.lmr14, fe vce(cluster precinct2014)
			matrix A[`y',1] = _b[std_treat]
			matrix A[`y',2] = _b[std_treat] + 1.96*_se[std_treat]
			matrix A[`y',3] = _b[std_treat] - 1.96*_se[std_treat]
			matrix A[`y',4] = `y'
		}
	svmat A

	rename A1 estimate2
	rename A2 upper2
	rename A3 lower2
	rename A4 n

graph twoway rcap upper2 lower2 n, lstyle(ci) ||  scatter estimate2 n, mstyle(p1) legend(off) graphregion(color(white)) ytitle(Coefficient Size) xtitle("")
graph export "C:\Users\henrik\Dropbox\IntregrationSD\Output\Tables\random_sdvotes.pdf", replace



